package cn.jishiyu11.xeqb.view

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView

import cn.jishiyu11.xeqb.R

/**
 * Created by vvguoliang on 2017/7/21.
 * 公共电话弹出框
 */

class PublicPhoneDialog(context: Context, themeResId: Int) : Dialog(context, themeResId) {

    class Builder(private val context: Context) {
        private var title: String? = null
        private var onClickListenerPhoneCancel: DialogInterface.OnClickListener? = null
        private var onClickListenerPhoneDetermine: DialogInterface.OnClickListener? = null
        private var msg = ""
        private var namecancel = ""
        private var namedetermine = ""

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        fun setTitle(title: String): PublicPhoneDialog.Builder {
            this.title = title
            return this
        }

        fun setTiltleMsg(msg: String): PublicPhoneDialog.Builder {
            this.msg = msg
            return this
        }

        fun setContentViewCancel(name: String, onClickListenerPhoneCancel: DialogInterface.OnClickListener): PublicPhoneDialog.Builder {
            this.namecancel = name
            this.onClickListenerPhoneCancel = onClickListenerPhoneCancel
            return this
        }

        fun setContentViewDetermine(name: String, onClickListenerPhoneDetermine: DialogInterface.OnClickListener): PublicPhoneDialog.Builder {
            this.namedetermine = name
            this.onClickListenerPhoneDetermine = onClickListenerPhoneDetermine
            return this
        }

        fun create(): PublicPhoneDialog {
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            // instantiate the dialog with the custom Theme
            val dialog = PublicPhoneDialog(context, R.style.Dialog)
            @SuppressLint("InflateParams") val layout = inflater.inflate(R.layout.dialog_phone, null)
            dialog.addContentView(layout, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT))

            dialog.setCanceledOnTouchOutside(false)
            dialog.setCancelable(false)

            val dialog_phone_title = layout.findViewById<TextView>(R.id.dialog_phone_title)
            val dialog_phone_context = layout.findViewById<TextView>(R.id.dialog_phone_context)
            val dialog_phone_button_cancel = layout.findViewById<Button>(R.id.dialog_phone_button_cancel)
            val dialog_phone_button_determine = layout.findViewById<Button>(R.id.dialog_phone_button_determine)

            if (!TextUtils.isEmpty(title)) {
                dialog_phone_title.text = title
            }

            if (!TextUtils.isEmpty(msg)) {
                dialog_phone_context.text = msg
            }

            if (onClickListenerPhoneCancel != null) {
                dialog_phone_button_cancel.text = namecancel
                dialog_phone_button_cancel.setOnClickListener { onClickListenerPhoneCancel!!.onClick(dialog, DialogInterface.BUTTON_POSITIVE) }
            } else {
                dialog_phone_button_cancel.visibility = View.GONE
            }

            if (onClickListenerPhoneDetermine != null) {
                dialog_phone_button_determine.text = namedetermine
                dialog_phone_button_determine.setOnClickListener { onClickListenerPhoneDetermine!!.onClick(dialog, DialogInterface.BUTTON_NEGATIVE) }
            } else {
                dialog_phone_button_determine.visibility = View.GONE
            }
            dialog.setContentView(layout)
            return dialog
        }
    }
}
